package org.dromara.system.kernel;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.system.constants.OrderConstants;
import org.dromara.system.domain.PayOrder;
import org.dromara.system.domain.PayOrderLog;
import org.dromara.system.domain.bo.PayGroupTypeQuery2Bo;
import org.dromara.system.domain.bo.aparams.AOrderBo;
import org.dromara.system.domain.bo.config.ApiConfig;
import org.dromara.system.domain.vo.PayTypeOut2Vo;
import org.dromara.system.domain.vo.PushOrderVo;
import org.dromara.system.service.IPayOrderLogService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class PayVersion2Kernel {

    private final OrderInsertKernel orderInsertKernel;

    private final IPayOrderLogService orderLogService;

    private final PayGroupKernel payGroupKernel;

    /**
     * 获取支付方式
     */
    public R<List<PayTypeOut2Vo>> payType(PayGroupTypeQuery2Bo bo, HttpServletRequest request) {
        String secretKey = request.getHeader(OrderConstants.secret_key);
        String appId = request.getHeader(OrderConstants.app_id);

        if (StringUtils.isEmpty(secretKey)) {
            return R.fail("client secret key is empty");
        }

        bo.setAppId(Long.valueOf(appId));
        bo.setSecretKey(secretKey);

        //获取应用配置的支付平台
        return payGroupKernel.selectPayGroupType(bo);
    }

    /**
     * 推送订单
     */
    public R<?> push(AOrderBo bo, HttpServletRequest request) {

        String secretKey = request.getHeader(OrderConstants.secret_key);
        String appId = request.getHeader(OrderConstants.app_id);
        bo.setAppId(Long.valueOf(appId));

        if (StringUtils.isEmpty(secretKey)) {
            return R.fail("client secret key is empty");
        }

        //新增一条订单日志
        PayOrderLog log = orderLogService.saveLog(null, null, orderInsertKernel.generateOrderNumber(), bo.getOrderAid(), "推送订单", JSONObject.toJSONString(bo, JSONWriter.Feature.WriteMapNullValue), null, System.currentTimeMillis());
        if (log == null) {
            return R.fail("Failed to add order log");
        }
        bo.setOrderNo(log.getOrderNo());
        R<PushOrderVo> result = orderInsertKernel.pushOrder2(bo, secretKey, log);

        PushOrderVo pushVo = result.getData();
        PayOrder order = null;
        ApiConfig.PayMethod method = null;
        if (pushVo != null) {
            order = pushVo.getOrder();
            method = pushVo.getPayMethod();
        }

        JSONObject data = new JSONObject();
        R<?> res;
        if (R.isSuccess(result) && order != null) {
            data.put("orderNo", order.getOrderNo());
            data.put("createTime", order.getCreateTime());
            data.put("type", method.getInnerType() != null ? "inner" : "direct");
            data.put("link", order.getLink());
            res = R.ok(data);
        } else {
            res = R.fail(result.getMsg());
        }

        orderLogService.updateLog(log.getLogId(), order != null ? order.getOrderId() : null, JSONObject.toJSONString(res, JSONWriter.Feature.WriteMapNullValue), System.currentTimeMillis() - log.getTime());

        return res;
    }
}
